"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : security-overflow
Case Name   : CREATE TABLE 创建分区表时分区表名超长是否会造成缓冲区溢出
Create At   : 2023/06/05
Owner       : opentestcase033
Description :
    1. 创建分区表名超长的分区表
    2. 向分区表中插入数据
    3. 清理环境
Expect      :
    1. 成功
    2. 成功
    3. 成功
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Overflow(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node('PrimaryDbUser')
        self.cons = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.table = 't_memory_0014'

    def test_security(self):
        text1 = '-----step1:创建分区表名超长的分区表;expect:成功-----'
        self.log.info(text1)
        random_str = 'a' * 16000
        sql = f'''drop table if exists {self.table} cascade;
        create table {self.table}
        ( number integer, name char(20), grade integer)
        partition by range(grade)
        (
          partition pass_{random_str} start(60) end(90),
          partition excellent_{random_str} start(90) end(maxvalue)
        );'''
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.cons.CREATE_TABLE_SUCCESS, msg, f'执行失败:{text1}')

        text2 = '-----step2:向分区表中插入数据;expect:成功-----'
        self.log.info(text2)
        msg = self.pri_sh.execut_db_sql(
            f"insert into {self.table} values('210101', 'Alan', 92);")
        self.log.info(msg)
        self.assertIn(self.cons.INSERT_SUCCESS_MSG, msg, f'执行失败：{text2}')

    def tearDown(self):
        text3 = '-----step3:清理数据;expect:成功-----'
        self.log.info(text3)
        sql_cmd = f"drop table if exists {self.table} cascade;"
        msg = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.cons.DROP_TABLE_SUCCESS, msg, f"执行失败:{text3}")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
